home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / snippets / Helper.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.9 KB  |  169 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import string
  5. from xml.sax import saxutils
  6. from ElementTree import *
  7. import re
  8. import gtk
  9. from gtk import gdk
  10.  
  11. def message_dialog(par, typ, msg):
  12.     d = gtk.MessageDialog(par, gtk.DIALOG_MODAL, typ, gtk.BUTTONS_OK, msg)
  13.     d.set_property('use-markup', True)
  14.     d.run()
  15.     d.destroy()
  16.  
  17.  
  18. def compute_indentation(view, piter):
  19.     line = piter.get_line()
  20.     start = view.get_buffer().get_iter_at_line(line)
  21.     end = start.copy()
  22.     ch = end.get_char()
  23.     while ch.isspace() and ch != '\r' and ch != '\n' and end.compare(piter) < 0:
  24.         if not end.forward_char():
  25.             break
  26.         
  27.         ch = end.get_char()
  28.     if start.equal(end):
  29.         return ''
  30.     return start.get_slice(end)
  31.  
  32.  
  33. def markup_escape(text):
  34.     return saxutils.escape(text)
  35.  
  36.  
  37. def spaces_instead_of_tabs(view, text):
  38.     if not view.get_insert_spaces_instead_of_tabs():
  39.         return text
  40.     return text.replace('\t', view.get_tab_width() * ' ')
  41.  
  42.  
  43. def insert_with_indent(view, piter, text, indentfirst = True, context = None):
  44.     text = spaces_instead_of_tabs(view, text)
  45.     lines = text.split('\n')
  46.     view.get_buffer().set_data('GeditSnippetsPluginContext', context)
  47.     if len(lines) == 1:
  48.         view.get_buffer().insert(piter, text)
  49.     else:
  50.         indent = compute_indentation(view, piter)
  51.         text = ''
  52.         for i in range(0, len(lines)):
  53.             if indentfirst or i > 0:
  54.                 text += indent + lines[i] + '\n'
  55.                 continue
  56.             text += lines[i] + '\n'
  57.         
  58.         view.get_buffer().insert(piter, text[:-1])
  59.     view.get_buffer().set_data('GeditSnippetsPluginContext', None)
  60.  
  61.  
  62. def get_buffer_context(buf):
  63.     return buf.get_data('GeditSnippetsPluginContext')
  64.  
  65.  
  66. def snippets_debug(*s):
  67.     pass
  68.  
  69.  
  70. def write_xml(node, f, cdata_nodes = ()):
  71.     if not node is not None:
  72.         raise AssertionError
  73.     if not hasattr(f, 'write'):
  74.         f = open(f, 'wb')
  75.     
  76.     f.write("<?xml version='1.0' encoding='utf-8'?>\n")
  77.     _write_node(node, f, cdata_nodes)
  78.  
  79.  
  80. def _write_indent(file, text, indent):
  81.     file.write('  ' * indent + text)
  82.  
  83.  
  84. def _write_node(node, file, cdata_nodes = (), indent = 0):
  85.     tag = node.tag
  86.     if node is Comment:
  87.         _write_indent(file, '<!-- %s -->\n' % _escape_cdata(node.text), indent)
  88.     elif node is ProcessingInstruction:
  89.         _write_indent(file, '<?%s?>\n' % _escape_cdata(node.text), indent)
  90.     else:
  91.         items = node.items()
  92.         if items and node.text or len(node):
  93.             _write_indent(file, '<' + tag.encode('utf-8'), indent)
  94.             if items:
  95.                 items.sort()
  96.                 for k, v in items:
  97.                     file.write(' %s="%s"' % (k.encode('utf-8'), _escape_attrib(v)))
  98.                 
  99.             
  100.             if node.text or len(node):
  101.                 file.write('>')
  102.                 if node.text and node.text.strip() != '':
  103.                     if tag in cdata_nodes:
  104.                         file.write(_cdata(node.text))
  105.                     else:
  106.                         file.write(_escape_cdata(node.text))
  107.                 else:
  108.                     file.write('\n')
  109.                 for n in node:
  110.                     _write_node(n, file, cdata_nodes, indent + 1)
  111.                 
  112.                 if not len(node):
  113.                     file.write('</' + tag.encode('utf-8') + '>\n')
  114.                 else:
  115.                     _write_indent(file, '</' + tag.encode('utf-8') + '>\n', indent)
  116.             else:
  117.                 file.write(' />\n')
  118.         
  119.         if node.tail and node.tail.strip() != '':
  120.             file.write(_escape_cdata(node.tail))
  121.         
  122.  
  123.  
  124. def _cdata(text, replace = string.replace):
  125.     text = text.encode('utf-8')
  126.     return '<![CDATA[' + replace(text, ']]>', ']]]]><![CDATA[>') + ']]>'
  127.  
  128.  
  129. def _escape_cdata(text, replace = string.replace):
  130.     text = text.encode('utf-8')
  131.     text = replace(text, '&', '&')
  132.     text = replace(text, '<', '<')
  133.     text = replace(text, '>', '>')
  134.     return text
  135.  
  136.  
  137. def _escape_attrib(text, replace = string.replace):
  138.     text = text.encode('utf-8')
  139.     text = replace(text, '&', '&')
  140.     text = replace(text, "'", ''')
  141.     text = replace(text, '"', '"')
  142.     text = replace(text, '<', '<')
  143.     text = replace(text, '>', '>')
  144.     return text
  145.  
  146.  
  147. def buffer_word_boundary(buf):
  148.     iter = buf.get_iter_at_mark(buf.get_insert())
  149.     start = iter.copy()
  150.     if not iter.starts_word():
  151.         start.backward_word_start()
  152.     
  153.     if not iter.ends_word():
  154.         iter.forward_word_end()
  155.     
  156.     return (start, iter)
  157.  
  158.  
  159. def drop_get_uris(selection):
  160.     lines = re.split('\\s*[\\n\\r]+\\s*', selection.data.strip())
  161.     result = []
  162.     for line in lines:
  163.         if not line.startswith('#'):
  164.             result.append(line)
  165.             continue
  166.     
  167.     return result
  168.  
  169.